package mest.controller.Modeling;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import mest.service.system.DBService;
import mest.util.MoneyUtil;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import mest.entity.AccountFile;
import mest.service.Modeling.AccountFileService;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 *@Title AccountFileController.java
 *@description 账户档案
 *@time 2019年12月16日 下午11:25:54
 *@author Twinson Wong
 **/
@RestController
@RequestMapping(value="/accountFile")
@CrossOrigin
public class AccountFileController {

	@Resource
	AccountFileService accountFileService;

	@Resource
	DBService dbService;
	
	/**
	 * 查询列表
	 * @param jsonObject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selectAll")
	public ResultObj selectAll(int index,int pageSize,String accountName, HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", index-1);
			map.put("pageSize", pageSize);
			map.put("accountName", accountName);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			List<AccountFile> list = accountFileService.selectAll(map);
			int count = accountFileService.count(map);
			ResultObj resultObj = ResultObj.createSuccess();
			resultObj.setCount(count);
			resultObj.setData(list);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 插入
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/insert")
	public ResultObj insert(@RequestBody JSONObject json, HttpServletRequest request){
		//		try {
		Map<String, Object> map = json.toJavaObject(Map.class);
		map.put("theirDb", mestUtil.getCompanyDB(request));
		ResultObj resultObj = accountFileService.insert(map);
		return resultObj;
		/*} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}*/
	}
	
	/**
	 * 修改
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/update")
	public ResultObj update(@RequestBody JSONObject json, HttpServletRequest request){
		//		try {
		Map<String, Object> map = json.toJavaObject(Map.class);
		map.put("theirDb", mestUtil.getCompanyDB(request));
		ResultObj resultObj = accountFileService.update(map);
		return resultObj;
		/*} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}*/
	}
	
	/**
	 * 删除
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete")
	public ResultObj delete(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			accountFileService.delete(map);
			return ResultObj.createSuccessMsg(ResultObj.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}


	/**
	 * 检查账户余额
	 *
	 * @return
	 */
	@PostMapping(value = "/check")
	public String check(@RequestParam Integer id, @RequestParam BigDecimal money) {
		ResultObj resultObj = null;
		String theirDb = dbService.getCompanyDB();
		try {
			AccountFile accountFile = accountFileService.selectById(id, theirDb);
			if (accountFile.getBalance().compareTo(money) >= 0){
				resultObj = ResultObj.builder().code(0).msg("余额充足").build();
			}else {
				resultObj = ResultObj.builder().code(-1).msg("余额不足").build();
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
		}
		return JSON.toJSONString(resultObj);
	}

	/**
	 * 付款操作
	 */
	@PostMapping(value = "/deduct")
	public String deduct(@RequestParam Integer id, @RequestParam BigDecimal money) {
		ResultObj resultObj = null;
		String theirDb = dbService.getCompanyDB();
		try {
			AccountFile accountFile = accountFileService.selectById(id, theirDb);
			BigDecimal accountFileMoney = MoneyUtil.subtract(accountFile.getBalance(), money);
			if (accountFileService.updateMoney(accountFile.getId(), accountFileMoney, theirDb) > 0) {
				resultObj = ResultObj.builder().code(0).msg("付款成功").build();
			} else {
				resultObj = ResultObj.builder().code(-1).msg("付款失败").build();
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
		}
		return JSON.toJSONString(resultObj);
	}

	/**
	 * 到账操作
	 */
	@PostMapping(value = "/add")
	public String add(@RequestParam Integer id, @RequestParam BigDecimal money){
		ResultObj resultObj = null;
		String theirDb = dbService.getCompanyDB();
		try {
			AccountFile accountFile = accountFileService.selectById(id, theirDb);
			BigDecimal accountFileMoney = MoneyUtil.add(accountFile.getBalance(), money);
			if (accountFileService.updateMoney(accountFile.getId(), accountFileMoney, theirDb) > 0) {
				resultObj = ResultObj.builder().code(0).msg("收款成功").build();
			} else {
				resultObj = ResultObj.builder().code(-1).msg("收款失败").build();
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
		}
		return JSON.toJSONString(resultObj);
	}


}
